package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.java.dao.PatientDao;
import com.java.entity.Patient;
import com.java.ext.MyButton;
import com.java.utils.MyWindowListener;

/**
* @ClassName: AddPatientWindow
* @Description: 添加患者界面
* @author ChenJiaLu
* @date 2023年12月29日 下午2:58:40
*
 */
public class AddPatientWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 父级窗口
	 */
	JFrame pJFrame;
	
	private Patient selectObj = null;

	// 构造函数  
    public AddPatientWindow(JFrame bJFrame, Patient patient) {
    	pJFrame = bJFrame;
    	selectObj = patient;
        // 设置窗口属性  
        setTitle(null == patient?"新增患者":"修改患者");  
		setSize(350, 270);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
        addWindowListener(new CloseButtonListener()); 
        setLocationRelativeTo(null);
  
        // 创建用于布局的 JPanel  
        JPanel contentPane = new JPanel();  
        contentPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));  
        contentPane.setLayout(new GridLayout(7, 2)); // 使用网格布局管理器  
  
        // 创建编号输入框  
        final JTextField numberField = new JTextField(120);
        numberField.setText(patient != null ?patient.getNumber(): "");
        contentPane.add(new JLabel("编号: "), new GridBagConstraints());  
        contentPane.add(numberField, new GridBagConstraints());  
  
        // 创建姓名输入框  
        final JTextField nameField = new JTextField(120);  
        contentPane.add(new JLabel("姓名: "), new GridBagConstraints());  
        nameField.setText(patient != null ?patient.getName(): "");
        contentPane.add(nameField, new GridBagConstraints());  
  
        // 创建性别输入框  
        final JComboBox<String> sexBox = new JComboBox<>(new String[]{"男", "女"});  
        contentPane.add(new JLabel("性别: "), new GridBagConstraints());  
        sexBox.setSelectedItem(patient != null ?patient.getSex(): "");  
        contentPane.add(sexBox, new GridBagConstraints());  
  
        // 创建年龄输入框  
        final JTextField ageField = new JTextField(120);  
        contentPane.add(new JLabel("年龄: "), new GridBagConstraints());  
        ageField.setText(patient != null ?String.valueOf(patient.getAge()): "");
        contentPane.add(ageField, new GridBagConstraints());  
  
        // 创建病情输入框  
        final JTextField diseaseField = new JTextField(120);  
        contentPane.add(new JLabel("病情: "), new GridBagConstraints());  
        diseaseField.setText(patient != null ?patient.getDisease(): "");
        contentPane.add(diseaseField, new GridBagConstraints());  
  
        // 创建密码输入框  
        final JTextField passwordField = new JTextField(120);  
        contentPane.add(new JLabel("密码: "), new GridBagConstraints());  
        passwordField.setText(patient != null ?patient.getPassword(): "");
        contentPane.add(passwordField, new GridBagConstraints());  
  
        // 创建重置按钮和提交按钮的面板  
        JPanel buttonPanel = new JPanel();  
        buttonPanel.setLayout(new FlowLayout()); // 使用流式布局，使按钮水平排列
        MyButton returnBtn = new MyButton("返回").defaultt();
        buttonPanel.add(returnBtn);  
        MyButton saveBtn = new MyButton("提交").primary();
        buttonPanel.add(saveBtn);  
  
        // 将按钮面板添加到主窗口的底部  
        getContentPane().add(contentPane, BorderLayout.CENTER);  
        getContentPane().add(buttonPanel, BorderLayout.SOUTH); // 添加到底部 
		
        returnBtn.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            	setVisible(false);
            	pJFrame.setEnabled(true);
            }  
        }); 
		
        saveBtn.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            	String number = numberField.getText();
            	String name = nameField.getText();
            	Object sex = sexBox.getSelectedItem();
            	String age = ageField.getText();
            	String disease = diseaseField.getText();
            	String password = passwordField.getText();
            	if("".equals(number)) {
            		warning("请输入编号");
            		return;
            	}
            	if("".equals(name)) {
            		warning("请输入名称");
            		return;
            	}
            	if("".equals(sex)) {
            		warning("请选中性别");
            		return;
            	}
            	if("".equals(age)) {
            		warning("请输入年龄");
            		return;
            	}
            	if("".equals(disease)) {
            		warning("请输入病情");
            		return;
            	}
            	if("".equals(password)) {
            		warning("请输入密码");
            		return;
            	}
            	PatientDao dDao = new PatientDao();
            	if(null == selectObj) {
            		selectObj = new Patient();
            		selectObj.setNumber(number);
            		selectObj.setName(name);
            		selectObj.setSex(sex.toString());
            		selectObj.setAge(Integer.valueOf(age));
            		selectObj.setDisease(disease);
            		selectObj.setPassword(password);
            		dDao.savePatient(selectObj);
            	}else {
            		selectObj.setNumber(number);
            		selectObj.setName(name);
            		selectObj.setSex(sex.toString());
            		selectObj.setAge(Integer.valueOf(age));
            		selectObj.setDisease(disease);
            		selectObj.setPassword(password);
            		dDao.updatePatient(selectObj);
            	}
            	ok("操作成功");
                dDao.close();
                setVisible(false);
                pJFrame.dispose();
            	new DoctorMainWindow();
            }  
        }); 
        
		setVisible(true);
		
    } 
    
    class CloseButtonListener extends MyWindowListener {
    	
    	@Override
    	public void windowClosing(WindowEvent e) {  
        	
        }
    }
      
}
